package com.worthtech.billportal.dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.worthtech.billportal.bean.AclUserRole;
import com.worthtech.billportal.bean.CsCtrlGroupmodule;
import com.worthtech.billportal.bean.CsCtrlUser;
import com.worthtech.billportal.bean.CsCtrlUsergroup;
import com.worthtech.billportal.dao.UserDAO;
import com.worthtech.billportal.exception.SCException;

public class UserHibernateDAO extends HibernateDaoSupport implements UserDAO {

	public CsCtrlUser getUser(String userseq) {
		List list = this.getHibernateTemplate().find(
				"from CsCtrlUser where userseq = ?", Long.parseLong(userseq));
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return (CsCtrlUser) list.get(0);
		}
	}

	public CsCtrlUser getUserByID(String userId) {
		List list = this.getHibernateTemplate().find(
				"from CsCtrlUser where userid = ? and VALIDFLAG = '1' ", userId);
		if (list == null || list.isEmpty()) {
			return null;
		} else {
			return (CsCtrlUser) list.get(0);
		}
	}

	public List getUserList(String organizationCode) {
		List list = this.getHibernateTemplate().find(
				"from CsCtrlUser where OrganizationCode = ?", organizationCode);
		return list;
	}

	public void updateUser(CsCtrlUser user) throws SCException {
		this.getHibernateTemplate().update(user);
	}

	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if (perNo == 0) {
			return this.getSession().createQuery(hql).list();
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

	public String getCount(String tableName, String condition) {
		String hql = "select count(*) from " + tableName + " " + condition;
		System.out.println("hql1=" + hql);
		String tmp = this.getSession().createQuery(hql).list().get(0)
				.toString();
		System.out.println("count=" + tmp);
		return tmp;
	}
	public String getSumBySQL(String list, String tableName, String condition) {
		String hql = "select sum(" + list + ") from " + tableName + " "
				+ condition;
		System.out.println("hql1=" + hql);
		List l = this.getSession().createSQLQuery(hql).list();
		if (l.size() == 0) {
			return "0";
		}
		if (l.get(0) == null) {
			return "0";
		}
		String tmp = l.get(0).toString();
		System.out.println("count=" + tmp);
		return tmp;
	}
	public void save(CsCtrlUsergroup c){
		this.getHibernateTemplate().save(c);
	}
	public void save(CsCtrlGroupmodule c){
		this.getHibernateTemplate().save(c);
	}
	public void del(CsCtrlGroupmodule c){
		this.getHibernateTemplate().delete(c);
	}
	public void update(CsCtrlUsergroup c){
		this.getHibernateTemplate().update(c);
	}
	public void update(CsCtrlGroupmodule c){
		this.getHibernateTemplate().update(c);
	}
	public void save(CsCtrlUser u) {
		this.getHibernateTemplate().save(u);
	}

	public void save(AclUserRole a) {
		this.getHibernateTemplate().save(a);
	}

	public void del(CsCtrlUser u) {
		this.getHibernateTemplate().delete(u);
	}

	public void del(AclUserRole u) {
		this.getHibernateTemplate().delete(u);
	}

	public String getSeqID(String seqname) {
		return this.getSession().createSQLQuery(
				"SELECT " + seqname + ".NEXTVAL FROM USER_SEQUENCES").list()
				.get(0).toString();
	}
}
